package cn.itcast.item.controller;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.hmall.pojo.item.ItemDoc;
import cn.itcast.item.mapper.ItemMapper;
import cn.itcast.item.service.ItemService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/item")
@Slf4j
public class ItemController {

    @Autowired
    private RabbitTemplate rab;

    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemMapper itemMapper;

    /**
     * 根据id查商品
     *
     * @param id
     * @return
     */
    @PutMapping("{id}")
    public ResultDTO findById(@PathVariable String id) {
        Item byId = this.itemService.getById(id);
        if (ObjectUtils.isEmpty(byId)) return ResultDTO.error("没查到");
        else return ResultDTO.ok(byId);
    }

    @PostMapping("/list")
    public PageDTO<Item> searchByPage(@RequestBody SearchItemDTO dto) {
        return this.itemService.getPage(dto);
    }

    @GetMapping("findAll")
    public List<Item> findAll() {
        return this.itemService.list();
    }

    @PostMapping
    public ResultDTO save(@RequestBody Item item) {

        Date date = new Date();
        item.setCreateTime(date);

        boolean b = this.itemService.saveOrUpdate(item);
        return b ? ResultDTO.ok() : ResultDTO.error();
    }

    @PutMapping
    public ResultDTO upDataItem(@RequestBody Item item) {
        Date date = new Date();
        item.setUpdateTime(date);


        boolean b = this.itemService.saveOrUpdate(item);
        return b ? ResultDTO.ok() : ResultDTO.error();
    }

    @PutMapping("/status/{id}/{status}")
    public ResultDTO updown(@PathVariable Long id, @PathVariable Integer status) {
        //根据上下架发消息
        String state = (status == 1 ? "up" : "down");
        if(state.equals("down"))
        rab.convertAndSend("item.exchange", state,id);
        else {
            Item item = itemMapper.selectById(id);
            ItemDoc itemDoc = new ItemDoc(item);
            String s = JSON.toJSONString(itemDoc);
            rab.convertAndSend("item.exchange", state,s);
        }

        return this.itemService.upAndDown(id, status);
    }

    @DeleteMapping("{id}")
    public ResultDTO deleteById(@PathVariable Long id) {
        ResultDTO resultDTO = this.itemService.deleteItemById(id);
        return resultDTO;
    }
    @GetMapping("{id}")
    public Item getById(@PathVariable Long id) {
        Item it = this.itemService.getItemById(id);
        return it;
    }

    @PostMapping("{id}")
    public Boolean decrease(@PathVariable String id){
        System.err.println("item的"+id);

        long l = Long.parseLong(id);

        System.out.println(l);
        Item itemById = this.itemService.getItemById(l);
        System.out.println(itemById);
        itemById.setStock((itemById.getStock()-1));
        int i = this.itemMapper.updateById(itemById);
        if(i==0)return false;else return true;
    }
    @PutMapping("/stock/{itemId}/{num}")
    public void decStock(@PathVariable String itemId,@PathVariable String num){
        long l = Long.parseLong(itemId);

        System.out.println(l);
        Item itemById = this.itemService.getItemById(l);
        System.out.println("之前 "+itemById.getStock());
        long l1 = Long.parseLong(num);
        int i1 = Integer.parseInt(num);
        itemById.setStock((itemById.getStock()-i1));
        int i = this.itemMapper.updateById(itemById);

        System.out.println("后来 "+this.itemService.getItemById(l).getStock());
    }
    @PutMapping("/we")
    public void comback(String id,Integer num){
        Item item = this.itemMapper.selectById(id);
        item.setStock(item.getStock()+num);
        QueryWrapper q=new QueryWrapper();
        q.eq("id",id);
        itemMapper.update(item,q);
    }
}
